//
//  SAAddVipLevelVC.m
//  saasRetail
//
//  Created by 黄志刚 on 2021/6/21.
//

#import "SAAddVipLevelVC.h"
#import "SASystemSettingsAddChildAPI.h"
#import "SASystemSettingsCheckAPI.h"
#import "SACommoditySpecialFeesModel.h"

@interface SAAddVipLevelVC ()<UITextFieldDelegate, HTTPAPIDelegate>

@property (nonatomic, strong) UITextField *levelTf;
@property (nonatomic, strong) UITextField *discountTf;
@property (nonatomic, strong) UITextField *growthTf;

@property (nonatomic, strong) NSString *idStr;
@property (nonatomic, strong) NSString *parentId;

@property (nonatomic, strong) SASystemSettingsAddChildAPI *systemSettingsAddChildAPI;
@property (nonatomic, strong) SASystemSettingsCheckAPI *systemSettingsCheckAPI;

@end

@implementation SAAddVipLevelVC

-(instancetype)initWithId:(NSString *)idStr AndParentId:(NSString *)ParentId {
    if (self = [super init]) {
        self.idStr = idStr;
        self.parentId = ParentId;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

#pragma mark - HTTPAPIDelegate
- (void)fetchData:(id)data fromServerSuccessedWithAPI:(HTTPAPI *)api {
    if (self.systemSettingsAddChildAPI == api) {
        [Prompt popPromptViewWithMsg:@"添加成功" duration:2];
        [self.navigationController popViewControllerAnimated:YES];
    }else if (self.systemSettingsCheckAPI == api) {
        [Prompt popPromptViewWithMsg:@"修改成功" duration:2];
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)fetchDataFromServerFailedWithError:(NSError *)error API:(HTTPAPI *)api {
    
}

-(void)clickedCompleteBtn {
    [self.view endEditing:YES];
    if (trimString(self.levelTf.text).length == 0) {
        [Prompt popPromptViewWithMsg:@"级别名称不能为空" duration:2];
        return;
    }else if (self.discountTf.text.length == 0) {
        [Prompt popPromptViewWithMsg:@"优惠折扣不能为空" duration:2];
        return;
    }else if (self.growthTf.text.length == 0) {
        [Prompt popPromptViewWithMsg:@"成长值不能为空" duration:2];
        return;
    }
    
    if (self.model != nil) {
        self.systemSettingsCheckAPI.name = self.levelTf.text;
        self.systemSettingsCheckAPI.settingsId = self.model.settingsId;
        self.systemSettingsCheckAPI.parentId = self.model.parentId;
        self.systemSettingsCheckAPI.value = [NSString stringWithFormat:@"%@,%zd,%0.lf",self.idStr,[self.discountTf.text integerValue],[self.growthTf.text doubleValue] * 100];
        [self.systemSettingsCheckAPI sendRequestToServerWithActivityViewAndMask:POST];
    }else {
        self.systemSettingsAddChildAPI.parentId = self.parentId;
        self.systemSettingsAddChildAPI.valueType = @"1";
        self.systemSettingsAddChildAPI.name = self.levelTf.text;
        self.systemSettingsAddChildAPI.value = [NSString stringWithFormat:@"%@,%zd,%0.lf",self.idStr,[self.discountTf.text integerValue],[self.growthTf.text doubleValue] * 100];
        [self.systemSettingsAddChildAPI sendRequestToServerWithActivityViewAndMask:POST];
    }
}

#pragma mark - UITextFieldDelegate
- (void)textFieldEditChanged:(UITextField*)textField {
    if (self.discountTf == textField) {
        NSInteger i = [textField.text integerValue];
        if (i > 100) {
            textField.text = @"100";
        }
    }else if (self.growthTf == textField) {
        double i = [textField.text doubleValue];
        textField.text = [NSString stringWithFormat:@"%0.lf",i];
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (string.length == 0) {
        return YES;
    }
    if (self.growthTf == textField) {
        if (textField.text.length > 9) {
            return NO;
        }
    }
    
    return YES;
}

-(void)initData {
    if (self.model != nil) {
        self.titleLab.text = @"修改级别";
        self.levelTf.text = self.model.name;
        NSArray *arr = [self.model.value componentsSeparatedByString:@","];
        if (arr.count > 2) {
            self.idStr = arr[0];
            self.discountTf.text = arr[1];
            double growth = [arr[2] doubleValue];
            self.growthTf.text = [NSString stringWithFormat:@"%0.lf",growth/100];
        }else {
            self.discountTf.text = @"";
            self.growthTf.text = @"";
        }
    }
}

-(void)initUI {
    [self setNavigationItemBackBBIAndTitle:@"添加级别"];
    [self setBgImage:nil];
    self.view.backgroundColor = kBgColor;
    
    UIButton *completeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    completeBtn.frame = CGRectMake(0, 0, kScaleNum(60), kScaleNum(44));
    [completeBtn setTitle:@"完成" forState:UIControlStateNormal];
    completeBtn.titleLabel.font = kFont(16);
    [completeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [completeBtn addTarget:self action:@selector(clickedCompleteBtn) forControlEvents:UIControlEventTouchUpInside];
    [self addRightViews:@[completeBtn]];
    
    UIView *whiteView = [[UIView alloc] initWithFrame:SARectMake(0, StatusHeight, kBaseScreenWidth, 121)];
    whiteView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:whiteView];
    
    NSArray *arr = @[@"级别名称", @"优惠折扣", @"成长值"];
    for (int i = 0; i < arr.count; i++) {
        UILabel *titleLab = [[UILabel alloc] initWithFrame:SARectMake(20, 41 * i, 80, 40)];
        titleLab.text = arr[i];
        titleLab.textColor = kBlackColor;
        titleLab.font = kFont(14);
        [whiteView addSubview:titleLab];
        
        UITextField *inputTf = [[UITextField alloc] initWithFrame:SARectMake(110, 41 * i, 200, 40)];
        inputTf.placeholder = @"请输入您的账号";
        inputTf.font = kFont(14);
        inputTf.textColor = kBlackColor;
        inputTf.textAlignment = NSTextAlignmentLeft;
        inputTf.clearButtonMode = UITextFieldViewModeAlways;
        inputTf.clearButtonMode = UITextFieldViewModeWhileEditing;
        inputTf.delegate = self;
        [inputTf addTarget:self action:@selector(textFieldEditChanged:) forControlEvents:UIControlEventEditingChanged];
        [whiteView addSubview:inputTf];
        
        UILabel *titleLab2 = [[UILabel alloc] initWithFrame:SARectMake(315, 41 * i, 60, 40)];
        titleLab2.textColor = kBlackColor;
        titleLab2.font = kFont(14);
        titleLab2.text = @"元";
        titleLab2.textAlignment = NSTextAlignmentCenter;
        [whiteView addSubview:titleLab2];
  
        UIView *line = [[UILabel alloc] initWithFrame:CGRectMake(0, kScaleNum(40 + 40 * i), kScreenWidth, 1)];
        line.backgroundColor = kLineColor;
        [whiteView addSubview:line];
        
        if (i == 0) {
            inputTf.placeholder = @"例如：VIP";
            inputTf.keyboardType = UIKeyboardTypeDefault;
            titleLab2.text = @"";
            self.levelTf = inputTf;
        }else if (i == 1) {
            inputTf.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
            inputTf.placeholder = @"8折输入80%";
            titleLab2.text = @"%";
            self.discountTf = inputTf;
        }else if (i == 2){
            inputTf.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
            inputTf.placeholder = @"达到该金额，自动升级到改等级";
            titleLab2.text = @"元";
            self.growthTf = inputTf;
        }
    }
}

-(SASystemSettingsAddChildAPI *)systemSettingsAddChildAPI {
    if (!_systemSettingsAddChildAPI) {
        _systemSettingsAddChildAPI = [SASystemSettingsAddChildAPI apiWithDelegate:self];
    }
    return _systemSettingsAddChildAPI;
}

-(SASystemSettingsCheckAPI *)systemSettingsCheckAPI {
    if (!_systemSettingsCheckAPI) {
        _systemSettingsCheckAPI = [SASystemSettingsCheckAPI apiWithDelegate:self];
    }
    return _systemSettingsCheckAPI;
}

@end
